package com.jiangli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jiangli.dto.EmployeeDTO;
import com.jiangli.dto.EmployeeLoginDTO;
import com.jiangli.entity.Employee;
import com.jiangli.exception.BaseException;
import com.jiangli.mapper.EmployeeMapper;
import com.jiangli.service.EmployeeService;

import com.jiangli.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AESUtil aesUtil;
    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();




        if(password==null||password.length()==0){
            throw  new BaseException("密码为空");
        }
        //密码比对
        try {
            //将前端的 加密密码 转为原始密码
            String originalPassword = AESUtil.decrypt(password);
            password=originalPassword;
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new BaseException("解密密码失败");
        }

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            // 账号不存在
            throw new BaseException("账号不存在");
        }


        //对解密的密码进行加密
         password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误;
            throw new BaseException("密码错误！");
        }

        if (employee.getStatus() == 0) {
            //账号被锁定
            throw new BaseException("账号被锁定");
        }

        //3、返回实体对象
        return employee;
    }

//
//    //新增员工
//    public void save(EmployeeDTO employeeDTO) {
//        //DTO只是一个间接接受数据的 类
//        //在调用DAO层的时候还是需要对应的对象进行存入
//        Employee employee=new Employee();
//        //拷贝数据 浅拷贝
//        BeanUtils.copyProperties(employeeDTO,employee);
//
//
//        //设置状态 初始状态为 1
//        employee.setStatus(StatusConstant.ENABLE);
//
//        //设置初始密码 123456
//        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
//
//        //设置创建时间和 修改时间
//       // employee.setCreateTime(LocalDateTime.now());
//        //employee.setUpdateTime(LocalDateTime.now());
//
//        //设置创建人
//
//        //employee.setCreateUser(BaseContext.getCurrentId());
//        //employee.setUpdateUser(BaseContext.getCurrentId());
//        employeeMapper.insert(employee);
//
//
//    }

    /**
     * 分页分类查询
     * @param employeePageQueryDTO
     * @return
     */
//    @Override
//    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
//        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
//
//        Page<Employee> page=employeeMapper.pageQuery(employeePageQueryDTO);
//
//        long total=page.getTotal();
//        List<Employee> records=page.getResult();
//        return new PageResult(total,records);
//    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     */
//    @Override
//    public void startOrStop(Integer status, Long id) {
//
//        //为了方便之后 Mapping方法不只是修改 状态 还可以修改其他 比如密码 年龄
//        //将数据封装 便于操作
//        Employee employee=new Employee();
//        employee.setId(id);
//        employee.setStatus(status);
//        employeeMapper.update(employee);
//    }

    /**
     * 根据id查找员工
     * @param
     * @return
     */
//    @Override
//    public Employee getById(Long id) {
//        Employee employee=employeeMapper.getById(id);
//        //密码没必要传给前端
//        employee.setPassword("****");
//        return employee;
//    }


 public void update(EmployeeDTO employeeDTO) {
   Employee employee=new Employee();
     String password = employee.getPassword();
     employee.setUsername(employeeDTO.getUsername());
   employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
   employee.setId(employeeDTO.getId());
     try {
         employeeMapper.update(employee);
     } catch (Exception e) {
         throw new BaseException("更新数据失败！");
     }


 }

}
